//
//  SmallDecisionsXjdAddItemViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/26.
//

#import "SmallDecisionsXjdAddItemViewController.h"
#import "SmallDecisionsTextView.h"

@interface SmallDecisionsXjdAddItemViewController ()

@property (nonatomic, strong) SmallDecisionsTextView *addItemsTextView;
@property (nonatomic, strong) UIButton *submitAddBtn;
@end

@implementation SmallDecisionsXjdAddItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加选项";
    [self addItemsTextView];
    [self submitAddBtn];
}

- (SmallDecisionsTextView *)addItemsTextView {
    if(!_addItemsTextView) {
        _addItemsTextView = [[SmallDecisionsTextView alloc] init];
        _addItemsTextView.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
        _addItemsTextView.backgroundColor = SmallDecisionsSecondBgColor;
        _addItemsTextView.layer.cornerRadius = 8;
        _addItemsTextView.layer.masksToBounds = YES;
        _addItemsTextView.layer.borderColor = SmallDecisionsThemeColor.CGColor;
        _addItemsTextView.layer.borderWidth = 1;
        _addItemsTextView.textColor = SmallDecisionsThemeFontColor;
        _addItemsTextView.font =  SmallDecisionsSetFontSize(14);
        _addItemsTextView.returnKeyType = UIReturnKeyDone;
        _addItemsTextView.placeholder = @"请输入选项，每行一个，例如：\n长沙\n杭州\n北京\n武汉";
        _addItemsTextView.placeholderColor = SmallDecisionsLineHeightColor;
        [self.view addSubview:_addItemsTextView];
        [_addItemsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(-40);
            make.height.mas_equalTo(300);
            make.centerY.mas_equalTo(-10);
        }];
    }
    return _addItemsTextView;
}

- (UIButton *)submitAddBtn {
    if(!_submitAddBtn) {
        _submitAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitAddBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_submitAddBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_submitAddBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateHighlighted];
        _submitAddBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
        _submitAddBtn.layer.cornerRadius = 25;
        _submitAddBtn.layer.masksToBounds = YES;
        _submitAddBtn.backgroundColor = SmallDecisionsThemeColor;
        [_submitAddBtn addTarget:self action:@selector(submitAddBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_submitAddBtn];
        [_submitAddBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.right.equalTo(self.addItemsTextView);
            make.top.equalTo(self.addItemsTextView.mas_bottom).offset(20);
        }];
    }
    return _submitAddBtn;
}

- (void)submitAddBtnAction {
    NSString *peopleNames = self.addItemsTextView.text;
    peopleNames = [peopleNames stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *names = [peopleNames componentsSeparatedByString:@"\n"];
    if (names.count > 2) {
        if (self.callback) {
            self.callback(names);
        }
    }
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
}

@end
